package com.thinkgem.jeesite.common.component.fck;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * ckEdit进行图片、Flash等上传辅助类
 */
@Controller
public class FckController{
	
	private static final String FLASH_PATH = "/upload/Flash/";
	private static final String MEDIA_PATH = "/upload/Media/";
	private static final String IMAGE_PATH = "/upload/Image/";
	private static final String PATH = "/upload/Image/";
	
	@RequestMapping(value="/fck/ckedit")
	public String fck(HttpServletRequest request, HttpServletResponse response){
		return "framework/tag/fckTest";
	}
	
	@RequestMapping(value="/fck/upload",method=RequestMethod.POST)
	public void fckUpload(String Type, HttpServletRequest request, HttpServletResponse response) {
		
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile uplFile = multipartRequest.getFileMap().entrySet().iterator().next().getValue();
//		System.out.println("文件名："+uplFile.getOriginalFilename());
		
		String filename = FilenameUtils.getName(uplFile.getOriginalFilename());
		String ext = FilenameUtils.getExtension(filename);
		
		//定义路径
		String path = "";
		if(Type.equals("Flash")){
			path = request.getRealPath("") + FLASH_PATH;
		}else if(Type.equals("Media")){
			path = request.getRealPath("") + MEDIA_PATH;
		}else if(Type.equals("Image")){
			path = request.getRealPath("") + IMAGE_PATH;
		}else{
			path = request.getRealPath("") + PATH;
		}
		String ctxpath = path + filename;
//		System.out.println("ctxpath："+ctxpath);
		PrintWriter out = null;
		try {
			File tmp = new File(path);
			if(!tmp.exists()){
				tmp.mkdirs();
			}
			File file = new File(tmp, filename);
			uplFile.transferTo(file);
			
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String retpath = request.getContextPath()+ IMAGE_PATH + filename;
		out.print(getFckOut(request,retpath));
		out.flush();
		out.close();
	}
	
	private String getFckOut(HttpServletRequest request,String url){
		StringBuffer sb = new StringBuffer(400);
		sb.append("<script type=\"text/javascript\">\n");
		sb.append("(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n");
		sb.append("window.parent.CKEDITOR.tools.callFunction(");
		sb.append("'").append(request.getParameter("CKEditorFuncNum")).append("'");
		sb.append(",");
		sb.append("'").append(url).append("'");
		sb.append(",");
		sb.append("''");
		sb.append(");\n");
		sb.append("</script>");
		return sb.toString();
	}

	
}
